package com.weikuang.fareast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.adapter.PersonAdapter;
import com.weikuang.fareast.bean.OperatorBean;
import com.weikuang.fareast.http.MainHttp;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class PersonActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.ed_number)
    TextView edNumber;
    @BindView(R.id.btn_goods_sure)
    Button btnGoodsSure;
    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;

    String hotel_id = "", office_id = "";
    String user_id = "";

    private PersonAdapter mPersonAdapter;
    OperatorBean.DataBean mUserdata = null;

    String type = "1";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        hotel_id = getIntent().getStringExtra("hotel_id");
        office_id = getIntent().getStringExtra("office_id");
        type = getIntent().getStringExtra("type");

        InitRecyclerView();
        operator();
        btnGoodsSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent();
                i.putExtra("data",mUserdata);
                setResult(3, i);
                finish();

            }
        });
    }

    private void InitRecyclerView() {
        mPersonAdapter = new PersonAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mPersonAdapter);

        mPersonAdapter.setOnItemClickListener(new PersonAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, OperatorBean.DataBean data, int position) {
                edNumber.setText(data.name);
                user_id = data.id;
                mUserdata = data;
            }
        });

    }

//    private void getUser() {
//        String keyword = edNumber.getText().toString();
//        MainHttp.get().user(hotel_id, office_id, keyword,"94")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(bindUntilEvent(ActivityEvent.DESTROY))
//                .subscribe(new OkObserver<UserBean>() {
//                    @Override
//                    public void onSuccess(UserBean hotelBean) {
//                        showOneToast(hotelBean.msg);
//                        mPersonAdapter.setNewData(hotelBean.data);
//                    }
//
//                    @Override
//                    public void onFailure(String message, Throwable tr) {
//                        ToastUtil.show(getActivity(), message);
//                    }
//                });
//    }

    private void operator() {
        String workType = "";
        if (!TextUtils.isEmpty(type)&&type.equals("2")){
            workType = "91,92,93";
        }
        MainHttp.get().operator(workType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<OperatorBean>() {
                    @Override
                    public void onSuccess(OperatorBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        mPersonAdapter.setNewData(hotelBean.data);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
}
